<?php
if (!defined('ABSPATH')) { exit; }

// 检查权限
global $wp_spider_monitor;
$is_premium = $wp_spider_monitor->is_premium_user();

global $wpdb;
$rules_table = $wpdb->prefix . 'spider_link_rules';
$rules = $wpdb->get_results("SELECT * FROM $rules_table ORDER BY id", ARRAY_A);
?>
<div class="wrap">
    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>类型</th>
                <th>启用</th>
                <th style="width:200px;">操作</th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($rules)): ?>
            <tr><td colspan="4">暂无规则</td></tr>
        <?php else: foreach ($rules as $r): ?>
            <tr data-id="<?php echo intval($r['id']); ?>">
                <td><?php echo intval($r['id']); ?></td>
                <td><?php echo esc_html($r['title']); ?></td>
                <td>
                    <?php $enabled = !empty($r['is_enabled']) ? 1 : 0; ?>
                    <span class="rule-status <?php echo $enabled? 'on' : 'off'; ?>"><?php echo $enabled? '已启用' : '已禁用'; ?></span>
                </td>
                <td>
                    <button class="button rule-toggle" data-enabled="<?php echo $enabled? '1':'0'; ?>" <?php echo $is_premium? '' : 'disabled'; ?>><?php echo $enabled? '禁用' : '启用'; ?></button>
                </td>
            </tr>
        <?php endforeach; endif; ?>
        </tbody>
    </table>
    <?php if (!$is_premium): ?>
        <div class="premium-restriction" style="margin-top:10px;">
            <span class="premium-icon">🔒</span>
            <span class="premium-text">链接规则修改需要激活许可证</span>
            <a href="<?php echo $wp_spider_monitor->get_license_page_url(); ?>" class="premium-link">立即激活</a>
        </div>
    <?php endif; ?>
</div>

<style>
/* 付费功能限制样式 */
.premium-restriction { display:flex; align-items:center; gap:8px; padding:10px 15px; background:linear-gradient(135deg,#667eea 0%, #764ba2 100%); color:#fff; border-radius:6px; font-size:14px; box-shadow:0 2px 8px rgba(102,126,234,.3); }
.premium-icon { font-size:16px; }
.premium-link { background:rgba(255,255,255,.2); color:#fff; padding:6px 12px; border-radius:4px; text-decoration:none; font-weight:600; border:1px solid rgba(255,255,255,.3); }
.rule-status.on { color:#46b450; font-weight:600; }
.rule-status.off { color:#d63638; font-weight:600; }
</style>

<script>
jQuery(function($){
    function notify(msg, type){
        var $n = $('<div class="notice notice-'+(type||'success')+' is-dismissible"><p>'+msg+'</p></div>');
        $('.wrap').prepend($n); setTimeout(function(){ $n.fadeOut(200, function(){ $(this).remove(); }); }, 2500);
    }
    function lockToggles(message){
        $('.rule-toggle').prop('disabled', true);
        if (message) notify(message, 'error');
    }
    function toggleRule($row, enable){
        $.post(ajaxurl, {
            action: 'spider_toggle_rule_status',
            nonce: '<?php echo wp_create_nonce('wp_spider_monitor_nonce'); ?>',
            id: $row.data('id'),
            is_active: enable ? 1 : 0
        }, function(resp){
            if (resp && resp.success){
                $row.find('.rule-status').toggleClass('on', enable).toggleClass('off', !enable).text(enable? '已启用':'已禁用');
                $row.find('.rule-toggle').data('enabled', enable? '1':'0').text(enable? '禁用':'启用');
                notify('规则状态更新成功','success');
            } else {
                var msg = (resp && resp.data && (resp.data.message || resp.data)) || '规则状态更新失败';
                // 令牌校验失败时，后端会返回 upgrade_url。发现时直接锁定按钮，禁止继续操作
                if (resp && resp.data && resp.data.upgrade_url) {
                    lockToggles(msg || '访问令牌无效或已过期，请在许可证页面激活');
                } else {
                    notify(msg, 'error');
                }
            }
        });
    }
    $(document).on('click', '.rule-toggle', function(e){ e.preventDefault(); var $tr=$(this).closest('tr'); var enabled = $(this).data('enabled')==='1'; toggleRule($tr, !enabled); });
});
</script>

